import requests
import shutil
import os
import pymongo
hero_url='https://g0.gph.netease.com/ngsocial/community/stzb/cfg/hero_extra.json?gameid=g10'
res = requests.get(hero_url)
data_json = res.json()
print(data_json[0].get('id'))
client = pymongo.MongoClient()
db = client.get_database('率土之滨')
table =db['武将']
table.insert_many(data_json)
client.close()

if not os.path.exists('static/率土之滨/武将库'):
    os.makedirs('static/率土之滨/武将库')
else:
    shutil.rmtree('static/率土之滨/武将库')
    os.makedirs('static/率土之滨/武将库')
for i in range(len(data_json)):
    hero_img = f'https://g0.gph.netease.com/ngsocial/community/stzb/cn/cards/cut/card_small_{data_json[i].get('id')}.jpg?gameid=g10'
    res_img =requests.get(hero_img)
    print(f'{data_json[i].get('name')}的照片')
    with open(f'static/率土之滨/武将库/{data_json[i].get("name")}.jpg','wb')as f:
        f.write(res_img.content)